package com.haotunet.app.goldtreasure.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.adapter.PickRecordAdapter;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.ExtractRecord;
import com.haotunet.app.goldtreasure.model.PickRecordModel;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout.OnRefreshListener;
import com.haotunet.app.pulltorefresh.pullableView.PullableListView;

import java.util.List;

public class PickRecordFragment extends BaseFragment {
	private View view;
	private PullableListView mLvPickRecord;
	private LinearLayout mNoRecord;
	private PullToRefreshLayout mfreshLayout;
	private boolean isPrepared;
	private boolean mHasLoadedOnce;
	private int pageNum = 20;
	private final int GET_EXTARCT_RECORD_SUCCESS = 1;
	private final int LOAD_MORE_PICK_RECORD_SUCCESS = 2;
	private Handler handle = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {

			case GET_EXTARCT_RECORD_SUCCESS:
				PickRecordModel model = (PickRecordModel) msg.obj;
				List<ExtractRecord> extractList = model.getItems();
				if (extractList.size() == 0) {
					mNoRecord.setVisibility(View.VISIBLE);
					return;
				}
				pickRecordAdapter = new PickRecordAdapter(getActivity(), extractList);
				mLvPickRecord.setAdapter(pickRecordAdapter);
				break;
			case LOAD_MORE_PICK_RECORD_SUCCESS:
				PickRecordModel moreModel = (PickRecordModel) msg.obj;
				if (moreModel.getItems().size() ==0) {
					UIHelper.showToast(getActivity(), getString(R.string.last_page_hint));
				}
				pickRecordAdapter.addDatas(moreModel.getItems());
				break;
			default:
				break;
			}
		}

	};
	private PickRecordAdapter pickRecordAdapter;

	public static PickRecordFragment newInstance(String type,int index) {
		Bundle args = new Bundle();
		args.putString("type", type);
		args.putInt("index", index);
		PickRecordFragment fragment = new PickRecordFragment();
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_pick_record, null);
		initView();
		isPrepared = true;
		if ("pickSuccess".equals(getArguments().getString("type"))||getArguments().getInt("index",0)==3){
			getPickGoldRecord("0", "");
		}
		return view;
	}

	private void getPickGoldRecord(String pagenum, final String action) {
		ActionUtil actionUtil = new ActionUtil(getActivity());
		actionUtil.getExtractGoldRecord(pagenum);
		actionUtil.setOnPassBackListener(new OnPassBackListener() {

			@Override
			public void passBack(BaseModel model) {
				if (!isActivityAvailable()) {
					return;
				}
				PickRecordModel pickModel = (PickRecordModel) model;
				if (pickModel == null) {
					UIHelper.showToast(getActivity(), getString(R.string.rank_fail));
					mfreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
					return;
				}
				if (pickModel.getErrorMsg() != null) {
					if (pickModel.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
						ActivityUtils.forceQuit(getActivity(),getString(R.string.crowding_in_tips_txt));
					} else {
						UIHelper.showToast(getActivity(), pickModel.getErrorMsg());
					}

				} else {
					mHasLoadedOnce = true;
					if (action.equals("refresh") || action.equals("loadmore")) {
						mfreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
					}
					Message msg = new Message();
					if (action.equals("loadmore")) {
						msg.what = LOAD_MORE_PICK_RECORD_SUCCESS;
						pageNum = pageNum + 20;
					} else {
						msg.what = GET_EXTARCT_RECORD_SUCCESS;
					}
					msg.obj = pickModel;
					handle.sendMessage(msg);
				}
			}
		});
	}

	private void initView() {
		mNoRecord = (LinearLayout) view.findViewById(R.id.no_trade_record_layout);
		mfreshLayout = (PullToRefreshLayout) view.findViewById(R.id.id_swipe_ly);
		mLvPickRecord = (PullableListView) view.findViewById(R.id.lv_pick_record);
		RefreshData(0);
	}

	private void RefreshData(final int pagenum) {
		mfreshLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
				getPickGoldRecord(pagenum + "", "refresh");
				pageNum = 20;
			}

			@Override
			public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
				getPickGoldRecord(pageNum + "", "loadmore");
			}
		});
	}

	@Override
	protected void lazyLoad() {
		if (!isPrepared || !isVisible || mHasLoadedOnce) {
			return;
		}
		getPickGoldRecord("0", "");

	}
}
